﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;
namespace WebApplication.Admin
{
    public partial class CongNoNCCList : System.Web.UI.Page
    {
        CHUCNANG objchucnang = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            KiemtraQUyenSuDung();
            if (!IsPostBack)
            {
                LoadHienthi();
            }
        }
        public void LoadHienthi()
        {
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            CONGNONCCRepository repository = new CONGNONCCRepository(new EFRepository<CONGNONCC>(), iunitfwork);
            IQueryable<CONGNONCC> oBjCongno = repository.All();
            grCongno.DataSource = oBjCongno;
            grCongno.DataBind();
        }

        protected void grCongno_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "lbtnsua")
            {
                Response.Redirect("CongNoNCCAdd.aspx?ID=" + e.CommandArgument);
            }
            if (e.CommandName == "lbtnxoa")
            {
                try
                {
                    IUnitOfWork iunitfwork = new EFUnitOfWork();
                    CONGNONCCRepository repository = new CONGNONCCRepository(new EFRepository<CONGNONCC>(), iunitfwork);
                    int id = int.Parse(e.CommandArgument.ToString());
                    CONGNONCC objCongno = repository.GetById(id);
                    if (objCongno != null)
                    {
                        repository.Delete(objCongno);
                        repository.Save();
                        LoadHienthi();
                    }
                }
                catch (Exception ex)
                {
                    RegisterClientScriptBlock("Thông báo", "<script> alert('Không thể xóa được')</script>");
                }
            }
        }
        public void KiemtraQUyenSuDung()
        {
            lbtnthemmoi.Visible = false;
            if (Session["MANV"] != null)
            {
                string strMANV = Session["MANV"].ToString();
                IUnitOfWork iunitwork = new EFUnitOfWork();
                lbtnthemmoi.Visible = false;
                NHANVIENRepository repository = new NHANVIENRepository(new EFRepository<NHANVIEN>(), iunitwork);
                NHANVIEN objNhanvien = repository.All().Where(p => p.MANV == strMANV).FirstOrDefault();
                if (objNhanvien != null)
                {
                    objchucnang = Commomcs.QuyenTruyCapChucnang(Request.Url.AbsolutePath, objNhanvien.MANV);
                    if (objchucnang != null)
                    {
                        if (objchucnang.ISADD == true)
                        {
                            lbtnthemmoi.Visible = true;
                        }
                        else
                        {
                            lbtnthemmoi.Visible = false;
                        }

                    }
                }
            }
        }
        protected void grCongno_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex < 0) return;
            CONGNONCC objcongno = ((CONGNONCC)e.Row.DataItem);
            if (objcongno != null)
            {
                Label lbnhacc = ((Label)e.Row.FindControl("lbnhacc"));
                if (lbnhacc != null)
                {
                    lbnhacc.Text = objcongno.NHACUNGCAP.TENNCC;
                }
                Label lbtinhtrang = ((Label)e.Row.FindControl("lbtinhtrang"));
                    if(objcongno.TINHTRANG==true)
                    {
                        lbtinhtrang.Text="Đã thanh toán";
                    }
                 if(objcongno.TINHTRANG==false)
                    {
                        lbtinhtrang.Text="Chưa thanh toán";
                    }
            }
            LinkButton lbtnsua = ((LinkButton)e.Row.FindControl("lbtnsua"));
            LinkButton lbtnxoa = ((LinkButton)e.Row.FindControl("lbtnxoa"));
            lbtnsua.Visible = false;
            lbtnxoa.Visible = false;
            if (objchucnang != null)
            {
                if (lbtnsua != null)
                {
                    if (objchucnang.ISEDIT == true)
                    {
                        lbtnsua.Visible = true;
                    }
                    else
                    {
                        lbtnsua.Visible = false;
                    }
                }
                if (lbtnxoa != null)
                {
                    if (objchucnang.ISDELETE == true)
                    {
                        lbtnsua.Visible = true;
                    }
                    else
                    {
                        lbtnxoa.Visible = false;
                    }
                }
            }

        }
    }
}